<?php


namespace addons\Minerva\api\modules\v1\controllers;

use common\helpers\ResultHelper;
use Yii;
use addons\Minerva\common\models\MinervaDepartment;
use api\controllers\OnAuthController;
use common\helpers\ArrayHelper;
use yii\data\ActiveDataProvider;

class DepartmentController extends OnAuthController
{
    /**
     * @var Member
     */
    public $modelClass = MinervaDepartment::class;

    public function actionIndex(){

        $list = new ActiveDataProvider([
            'query' => MinervaDepartment::find()
//                ->where(['userid' => $member_id])
                ->orderBy('created_at desc')
//                ->select('id,jiben_name,jiben_idcard,created_at,state')
                ->asArray(),
            'pagination' => [
                'pageSize' => $this->pageSize,
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
        $array = self::itemsMerge($list->getModels());
        return $array;
    }

    /**
     * 递归
     *
     * @param array $items
     * @param int $pid
     * @param string $idField
     * @param string $pidField
     * @param string $child
     * @return array
     */
    protected static function itemsMerge(array $items, $pid = 0)
    {
        $arr = [];
        foreach ($items as $v) {
            if (is_numeric($pid)) {
                if ($v['pid'] == $pid) {
                    $v['-'] = self::itemsMerge($items, $v['id']);
                    $arr[] = $v;
                }
            }
        }
        return $arr;
    }

    /**
     * 编辑/创建
     *
     * @return mixed
     */
    public function actionEdit()
    {
        $post = Yii::$app->request->post();
        $model = new MinervaDepartment();
        if($post['id']){
            $model = $model::find()
                ->where(['id' => $post['id']])
                ->one();
        }
        $post['pid']= Yii::$app->request->post('pid',null) ?? $model->pid; // 父id
        if($post['pid']){
            $parent = $model::findOne(['id' => $post['pid']]);
            $post['level'] = $parent['level'] + 1;
        }else{
            $post['level'] = 1;
        }
        $model->attributes = $post;
        if ($model->save()) {
            return $model;
        }else{
            return ResultHelper::json(422, $this->getError($model));
        }
    }

    /**
     * 编辑/创建
     *
     * @return mixed
     */
    public function actionDetail()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        $model->pid = Yii::$app->request->get('pid', null) ?? $model->pid; // 父id
        $model->level = Yii::$app->request->get('level')+1;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->referrer();
        }
//        $one_level = $model::find()->select(['id','title'])->indexBy('id')->asArray()->all();
        $one_level = $model::find()->asArray()->all();
        $list = ArrayHelper::removeByValue($one_level, $id);
        $models = ArrayHelper::itemsMerge($list);
        $data = ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
        $dropDownList = ArrayHelper::merge([0 => '顶级部门'], $data);

        return ['dropDownList' => $dropDownList, 'model' => $model];
    }
}